package cn.xxm.strategy.pay.test;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class Client {
    public static void main(String[] args) {
        Main main1 = Main.builder().name("main1").Type("dog1").build();
        Main main2 = Main.builder().name("main2").Type("dog2").build();
        Add add1 = Add.builder().name("add1-1").mainType("dog1").build();
        Add add2 = Add.builder().name("add2-1").mainType("dog2").build();
        Add add3 = Add.builder().name("add2-2").mainType("dog2").build();
        List lcpolList = new ArrayList<>();
        lcpolList.add(main1);
        lcpolList.add(main2);
        lcpolList.add(add1);
        lcpolList.add(add2);
        lcpolList.add(add3);

        System.out.println("JSONObject.toJSON(lcpolList) = " + JSONObject.toJSON(lcpolList));
        List<Main> animalList = new ArrayList<>();
        Map<String, Main> map = new HashMap<>();
        for (Object o : lcpolList) {
            if(o instanceof Main){
                Main animal = (Main) o;
                map.put(animal.getType(),animal);
                animalList.add(animal);
            }
        }

        for (Object o : lcpolList) {
            if(o instanceof Add){
                Add add = (Add)o;
                Main animal = map.get(add.getMainType());
                List<Add> addList = animal.getAddList();
                if(CollectionUtils.isEmpty(addList)){
                    addList = new ArrayList<>();
                }
                addList.add(add);
                animal.setAddList(addList);
            }

        }
        System.out.println("animalList = " + JSONObject.toJSON(animalList));
    }
}
